//
//  WaterMarkPolicyManager.m
//  gdemm
//
//  Created by liyuanyuan on 16/6/8.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "WaterMarkPolicyManager.h"
#import "UserDefaultsUtil.h"
#import "Policies.h"
#import "DeviceInfo.h"

@interface WaterMarkPolicyManager ()


@property(nonatomic,assign)NSInteger userName;

@property(nonatomic,assign)NSInteger ip;

@property(nonatomic,copy)NSString * manualText;

@property(nonatomic,copy)NSString * text;

@property(nonatomic,copy)NSString * tmplText;

@property(nonatomic,assign)BOOL mIsStar;

@end

@implementation WaterMarkPolicyManager

-(instancetype)init{
    if (self = [super init]) {
        self.ip = 0;
        self.userName = 0;
        self.manualText = nil;
        self.mIsStar = NO;
    }
    return self;
}

#pragma  mark 单例的实现
static WaterMarkPolicyManager * manager;

+(instancetype)getInstence{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[self alloc]init];
    });
    return manager;
}

#pragma mark 实现策略处理中的协议方法
/**
 * 策略处理器能处理哪些策略
 * @return 返回支持的策略
 */
-(NSString *)supportPolicy{
    return WATERMARK;
}

/**
 * @brief 策略开启时会被调用
 * @param 参数1策略类型
 * @param 参数2策略附带的参数
 * @return 返回YES
 */
- (BOOL)startPolicyWithPolicyType:(NSString *)policyType policyContent:(NSDictionary *)policyContent{
    self.ip = 0;
    self.userName = 0;
    self.manualText = nil;
    self.mIsStar = YES;
    self.text = nil;
    if (policyContent != nil && policyContent.count  > 0) {
        if (policyContent[@"userName"] != nil) {
            self.userName = [policyContent[@"userName"] intValue];
        }
        if (policyContent[@"ip"] != nil) {
            self.ip = [policyContent[@"ip"] intValue];
        }
        if (policyContent[@"manualText"] != nil) {
            self.manualText = policyContent[@"manualText"];
        }
    }
    [[UserDefaultsUtil shareInstance] saveBoolConfig:LC_OPENWATERMARKPOLICY :YES];
    return YES;

}

/**
 * 策略关闭时会被调用
 * @param 参数策略类型
 * @return 返回NO
 */
-(BOOL)stopPolicyWithPolicyType:(NSString *)policyType{
    self.text = nil;
    self.mIsStar = NO;
    [[UserDefaultsUtil shareInstance] saveBoolConfig:LC_OPENWATERMARKPOLICY :NO];
    return  NO;
}

#pragma mark 实现任务管理器的中的协议方法
/**
 * 执行策略时被调用
 */
-(void)run{
    NSString * tempUserName;
    NSString * tempIp;
    
    if (self.userName == 1) {
        tempUserName = [[UserDefaultsUtil shareInstance]getString:LC_USERNAME];
    }else{
        tempUserName = @"";
    }
    
    if (self.ip== 1) {
        tempIp = [DeviceInfo getIpAddress];
    }else{
        tempIp = @"";
    }
    
    if (self.manualText != nil) {
        self.manualText = [NSString stringWithFormat:@"%@",self.manualText];
    }else{
       self.manualText = @"";
    }
    self.text = [NSString stringWithFormat:@"%@ %@ %@",tempUserName,tempIp,self.manualText];
    if (![self.text isEqualToString:self.tmplText]) {
        self.tmplText = self.text;
        [[UserDefaultsUtil  shareInstance]saveStringConfig:LC_WATRMARKTEXT :self.tmplText];
    }
}

/**
 * 用来判断策略是否打开
 * @return 是否支持该策略策略
 */
-(BOOL)valid{
    return self.mIsStar;
}


@end
